<template>
  <div class="login_container">
    <div class="login_box">
      <div class="img_box">
        <img src="@/assets/logo.png">
      </div>
      <el-form :model="LoginForm"
               status-icon
               label-width="100px"
               class="login_form"
               :rules="rules"
               ref="Loginref">
        <el-form-item label="账号"
                      prop="username">
          <el-input type="text"
                    prefix-icon="el-icon-user-solid"
                    v-model="LoginForm.username"
                    placeholder="请输入账号"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码"
                      prop="password">
          <el-input type="password"
                    prefix-icon="el-icon-unlock"
                    v-model="LoginForm.password"
                    placeholder="请输入密码"
                    autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item class="btns">
          <el-button type="primary"
                     @click="submitForm()">提交</el-button>
          <el-button @click="resetForm( )">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      LoginForm: {
        password: '',
        username: ''
      },
      rules: {
        pass: [
          { required: true, message: '请输入密码', trigger: 'blur' }

        ],
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 8, message: '登陆账号3-8位', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async submitForm () {
      var _this = this
      const { data: res } = await this.$http.post('/login', this.LoginForm)
      if (res.meta.status === 200) {
        _this.$msg({
          message: '恭喜您，登陆成功',
          type: 'success'
        })
        _this.$router.push('home')
        window.sessionStorage.setItem('token', res.data.token)
      } else {
        _this.$msg({
          message: '很遗憾，登陆失败',
          type: 'error'
        })
      }
    },
    resetForm () {
      this.$refs.Loginref.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  width: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .img_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
